# -*- coding: utf-8 -*-

class Files < Control
  
  def initialize(type)
    super("sites")
    @type = type
    @dir = "../html/#{type}/"
  end
  
  def add_page_values(values)
    
    site = Sites.new()
    values = site.page_default_settings(values)
    
    menu = Categorys.new()
    values["menu"] = menu.get_menu("items.rb")
    
    #html = @dir
    html = self.get_upload_form()
    html += self.save_uploaded_file()
    html += self.get_file_list_view()
    
    values["titlebar"] = @title + " - " + values["title"]
    values["contents"] = html
    
    return values
    
  end
  
  def get_upload_form()
    
    values = {}
    
    if @type == "images" then
      values["title"] = "写真・画像"
    else
      values["title"] = "ファイル・その他"
    end
    
    @title = values["title"]
    html = @vw.load(values, $templete_dir + "files_upload.html")
    
    return html
    
  end
  
  def save_uploaded_file()
    
    html = ""
    
    if $_POST.has_key?("file") then
      
      file = $_POST["file"][0]
      
      if file.size > 0 then
        
        #html += $cgi.params["sample"][0].read
        #html += $_POST["sample"][0].read
        
        filename = file.original_filename.split(/(\\|\/)/)[-1]
        
        html = "<p>アップロードしました。</p>"
        
        open(@dir + filename, "w") {|fh|
          fh.binmode
          fh.write file.read
        }
        
      end
      
    end
    
    return html
    
  end
  
  def get_file_list_view()
  
    html = ""
    
    get_files_array().each {|f|
      url = @dir + f.to_s
      html += <<EOF
<div id="#{f}">
  #{self.get_file_list_view_item(f, url)}
</div>
EOF
    }
    
    return html
    
  end
  
  def get_image_for_list(f, url)
    
    if @type == "images" then
      img = "<a href='#{url}' rel='lightbox' ><img src='#{url}' width=100 height=100 border=1 /></a>"
    else
      img = "<a href='#{url}' target='_blank'><img src='images/file_image.gif' width=100 height=100 border=0 /></a>"
    end
    
    #img = "#{@type}<br>#{f}<br>#{url}"
    
    return img
    
  end
  
  def get_sample_html_tag(f, url)
    
    if @type == "images" then
      html = "&lt;img src='#{url}' />"
    else
      html = "&lt;a href='#{url}'>#{f}&lt;/a>"
    end
    
    #img = "#{@type}<br>#{f}<br>#{url}"
    
    return html
    
  end
  
  def get_file_list_view_item(f, url)
    
    html = <<EOF
  <table width=100%>
    <tr>
      <td width="100px">#{self.get_image_for_list(f, url)}</td>
      <td>
        #{f.to_s}
<!--
        <div style="font-size: x-small">
          #{self.get_sample_html_tag(f, url)}
        </div>
-->
      </td>
      <td align="right" nowrap>
        <input type="button" value="名前変更" onclick="get_change_filename_form('#{f}')" />
        <input type="button" value="削除" onclick="get_delete_file_form('#{f}')" />
      </td>
    </tr>
  </table>
EOF
    
    return html
    
  end
  
  def get_edit_filename_form()
  
    f = $_POST["file"].to_s
    url = @dir + f
    
    html = <<EOF
<form>
  <input type="hidden" name="mode" value="update" />
  <input type="hidden" name="org" value="#{f}" />
  <table width=100%>
    <tr>
      <td width="100px">#{self.get_image_for_list(f, url)}</td>
      <td><input type="text" name="new" value="#{f}" /></td>
      <td align="right">
        <input type="button" value="変更" onclick="update_filename_by_form('#{f}', this.form)" />
        <input type="button" value="取消" onclick="file_operation_cancel('#{f}')" />
      </td>
    </tr>
  </table>
</form>
EOF
    
    return html
    
  end
  
  def get_delete_confirmation_form()
  
    f = $_POST["file"].to_s
    url = @dir + f
    
    html = <<EOF
<form>
  <input type="hidden" name="mode" value="delete" />
  <input type="hidden" name="file" value="#{f}" />
  <table width=100%>
    <tr>
      <td width="100px">#{self.get_image_for_list(f, url)}</td>
      <td>
        #{f}
        <div style="font-size: small">
          このファイルを削除してよろしいですか?
        </div>
      </td>
      <td align="right">
        <input type="button" value="実行" onclick="delete_file_by_form('#{f}', this.form)" />
        <input type="button" value="取消" onclick="file_operation_cancel('#{f}')" />
      </td>
    </tr>
  </table>
</form>
EOF
    
    return html
    
  end
  
  def main()
    
    if $_POST.has_key?("mode") then
      proxy()
    else
      super()
    end
    
  end
  
  def make_page_contents(mode, id)
    return ""
  end
  
  def get_files_array()
    
    files = []
    i = 0
    
    Dir::foreach(@dir) {|f|
      next if /^\.+$/ =~ f
      files[i] = f
      i += 1
    }
    
    return files.sort
    
  end
  
  def get_file_select_form()
    
    dir = @dir
    html = "<table><tr>"
    
    # images
    @dir = "../html/images/"
    values = []
    i = 0
    self.get_files_array().each {|f|
      values[i] = f
      i += 1
    }
    
    if i > 0 then
      html += "<td>画像 : </td>"
      html += "<td>" + make_select_form_without_key("images", values, "") + "</td>"
      html += "<td><input type='button' value='挿入' onclick='insert_selected_image(this.form);' /></td>"
    end
    
    # files
    @dir = "../html/files/"
    values = []
    i = 0
    self.get_files_array().each {|f|
      values[i] = f
      i += 1
    }
    
    if i > 0 then
      html += "<td>データ : </td>"
      html += "<td>" + make_select_form_without_key("files", values, "") + "</td>"
      html += "<td><input type='button' value='挿入' onclick='insert_selected_file(this.form);' /></td>"
    end
    
    html += "</tr></table>"
    
    @dir = dir
    
    return html
    
  end
  
  def proxy()
  
    html_open()
    
    mode = $_POST["mode"].to_s
    #id = $_POST["id"]
    
    if mode == "edit" then
      html = self.get_edit_filename_form()
      
    elsif mode == "update" then
      org = @dir + $_POST["org"].to_s
      new = @dir + $_POST["new"].to_s
      File.rename(org, new)
      html = "<p>File name is changed.</p>"
      #f = $_POST["file"]
      #url = @dir + f.to_s
      #html = self.get_file_list_view_item(f, url)
      
    elsif mode == "conf" then
      html = self.get_delete_confirmation_form()
      
    elsif mode == "cancel" then
      f = $_POST["file"]
      url = @dir + f.to_s
      html = self.get_file_list_view_item(f, url)
      
    elsif mode == "delete"
      file = @dir + $_POST["file"].to_s
      File.delete(file)
      html = "<p>'#{file}' is deleted.</p>"
      
    else
      html = mode.to_s
      
    end
    
    print html
    #print print_params()
    
    terminate()
    html_close()
    
  end
  
end
